package com.sdg.server.rest;

import com.sdg.server.model.domain.User;
import com.sdg.server.model.recom.Recommendation;
import com.sdg.server.model.request.*;
import com.sdg.server.service.MovieService;
import com.sdg.server.service.RatingService;
import com.sdg.server.service.RecommenderService;
import com.sdg.server.service.MyUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 用于处理电影相关的功能
 */
@RequestMapping("/rest/movie")
@Controller
public class MovieRestApi {

    private Logger logger = LoggerFactory.getLogger(MovieRestApi.class);
    @Autowired
    private MyUserService myUserService;
    @Autowired
    private RecommenderService recommenderService;
    @Autowired
    private MovieService movieService;
    @Autowired
    private RatingService ratingService;
    /*************************首页功能开始*********************************/
    /**
     * 首页推荐
     * 混合推荐
     * 获取和推荐，总共推荐10个
     * 实时推荐6  离线推荐 4个
     *
     * @param username 用户名称
     * @param num      10
     * @param model
     * @return
     */
    @RequestMapping(value = "/guess", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Model getGuessMovies(@RequestParam("username") String username, @RequestParam("num") int num, Model model) {
        //用户登录判断
        User user = myUserService.findByUsername(username);
        //1.查询到的推荐结果
        List<Recommendation> recommendations = recommenderService.getHybridRecommendations(new MovieHybridRecommendationRequest(user.getUid(), num));
        //2.
        if (recommendations.size() == 0) {
            String randomGenres = user.getPrefGenres()
                    .get(new Random().nextInt(user.getPrefGenres().size()));
            //如果通过上述的查询方法没有查到数据，我们就去topn 中的数据[类别 个数]
            recommendations = recommenderService.getTopGenresRecommendations(new TopGenresRecommendationRequest(randomGenres.split(" ")[0], num));
        }
        model.addAttribute("success", true);
        //返回结果
        model.addAttribute("movies", movieService.getHybirdRecommendeMovies(recommendations));
        return model;
    }

    /**
     * 获取离线推荐的电影
     *
     * @param username
     * @param num
     * @param model
     * @return
     */
    @RequestMapping(value = "/offline", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Model getofflinetimeRecommendations(@RequestParam("username") String username, @RequestParam("num") int num, Model model) {
        User user = myUserService.findByUsername(username);
        //这个参数传递的不正确
        List<Recommendation> recommendations = recommenderService.findMovieCFRecs(27783, num);
        model.addAttribute("success", true);
        model.addAttribute("movies", movieService.getHybirdRecommendeMovies(recommendations));
        return model;
    }

    /**
     * 获取实时推荐的电影
     *
     * @param username
     * @param num
     * @param model
     * @return
     */
    @RequestMapping(value = "/stream", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Model getRealtimeRecommendations(@RequestParam("username") String username, @RequestParam("num") int num, Model model) {
        User user = myUserService.findByUsername(username);
        List<Recommendation> recommendations = recommenderService.findStreamRecs(user.getUid(), num);

        model.addAttribute("success", true);
        model.addAttribute("movies", movieService.getHybirdRecommendeMovies(recommendations));
        return model;
    }

    /**
     * 我喜欢的电影
     *
     * @param username
     * @param num
     * @param model
     * @return
     */
    @RequestMapping(value = "/wish", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Model getWishMovies(@RequestParam("username") String username, @RequestParam("num") int num, Model model) {
        User user = myUserService.findByUsername(username);
        List<Recommendation> recommendations = recommenderService.getCollaborativeFilteringRecommendations(new UserRecommendationRequest(user.getUid(), num));
        if (recommendations.size() == 0) {
            System.out.println("user.getPrefGenres().size() :" + user.getPrefGenres().size());
            String randomGenres = user.getPrefGenres().get(new Random().nextInt(user.getPrefGenres().size()));
            recommendations = recommenderService.getTopGenresRecommendations(new TopGenresRecommendationRequest(randomGenres.split(" ")[0], num));
        }
        model.addAttribute("success", true);
        model.addAttribute("movies", movieService.getRecommendeMovies(recommendations));
        return model;
    }


    //提供获取离线推荐信息的接口
    public Model getOfflineRecommendation(String userName, Model model) {
        return null;
    }

    /**
     * 获取热门推荐
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/hot", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Model getHotMovies(@RequestParam("num") int num, Model model) {
        List<Recommendation> recommendations = recommenderService.getHotRecommendations(new HotRecommendationRequest(num));
        model.addAttribute("success", true);
        model.addAttribute("movies", movieService.getRecommendeMovies(recommendations));
        return model;
    }

    /**
     * 获取投票最多的电影
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/rate", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Model getRateMoreMovies(@RequestParam("num") int num, Model model) {
        List<Recommendation> recommendations = recommenderService.getRateMoreRecommendations(new RateMoreRecommendationRequest(num));
        System.out.println(recommendations.size()+"----------------------c");
        model.addAttribute("success", true);
        model.addAttribute("movies", movieService.getRecommendeMovies(recommendations));
        return model;
    }

    /**
     * 获取新添加的电影
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/new", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Model getNewMovies(@RequestParam("num") int num, Model model) {
        model.addAttribute("success", true);
        System.out.println("new ---");
        model.addAttribute("movies", movieService.getNewMovies(new NewRecommendationRequest(num)));
        return model;
    }

    /**
     * 获取电影详细页面相似的电影集合
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/same/{id}", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Model getSameMovie(@PathVariable("id") int id, @RequestParam("num") int num, Model model) {
        List<Recommendation> recommendations = recommenderService.getCollaborativeFilteringRecommendations(new MovieRecommendationRequest(id, num));
        model.addAttribute("success", true);
        model.addAttribute("movies", movieService.getRecommendeMovies(recommendations));
        return model;
    }


    /**
     * 获取单个电影的信息
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/info/{id}", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Model getMovieInfo(@PathVariable("id") int id, Model model) {
        model.addAttribute("success", true);
        model.addAttribute("movie", movieService.findByMID(id));
        return model;
    }

    /**
     * 模糊查询电影
     *
     * @param query
     * @param model
     * @return
     */
    @RequestMapping(value = "/search", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Model getSearchMovies(@RequestParam("query") String query, Model model) {
        List<Recommendation> recommendations = recommenderService.getContentBasedSearchRecommendations(new SearchRecommendationRequest(query, 100));
        model.addAttribute("success", true);
        model.addAttribute("movies", movieService.getRecommendeMovies(recommendations));
        return model;
    }

    /**
     * 查询类别电影
     *
     * @param category
     * @param model
     * @return
     */
    @RequestMapping(value = "/genres", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Model getGenresMovies(@RequestParam("category") String category, Model model) {
        List<Recommendation> recommendations = recommenderService.getContentBasedGenresRecommendations(new SearchRecommendationRequest(category, 100));
        model.addAttribute("success", true);
        model.addAttribute("movies", movieService.getRecommendeMovies(recommendations));
        return model;
    }

    /**
     * 获取用户评分过得电影
     *
     * @param username
     * @param model
     * @return
     */
    @RequestMapping(value = "/myrate", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Model getMyRateMovies(@RequestParam("username") String username, Model model) {
        User user = myUserService.findByUsername(username);
        model.addAttribute("success", true);
        model.addAttribute("movies", movieService.getMyRateMovies(user.getUid()));
        return model;
    }
    /*************************首页功能结束*********************************/
    /**
     * @describe: 用户评分趋势
     * @Param: [username, model]
     * @Return: Model
     */
    @RequestMapping(value = "/stat", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Model getMyRatingStat(@RequestParam("username") String username, Model model) {
        //查出用户
        User user = myUserService.findByUsername(username);
        //设置成功
        model.addAttribute("success", true);
        /*这是原来的业务逻辑*/
        //model.addAttribute("stat", ratingService.getMyRatingStat(user));
        List<Integer> list =new ArrayList<>();
        /*初始值写死*/
        list.add(10);
        list.add(9);
        list.add(8);
        list.add(8);
        list.add(1);
        list.add(40);
        list.add(42);
        list.add(50);
        list.add(30);
        list.add(35);

       // model.addAttribute("stat", ratingService.getMyRatingStat(user));
        model.addAttribute("stat", list);
        return model;
    }

    /**
     * 电影系统用户展示
     * @param username
     * @param model
     * @return
     */
    @RequestMapping(value = "/draw", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Model getMyDraw(@RequestParam("username") String username, Model model) {
        //查出用户
        User user = myUserService.findByUsername(username);
        //设置成功
        model.addAttribute("success", true);
        /*这是原来的业务逻辑*/
        //model.addAttribute("stat", ratingService.getMyRatingStat(user));
        List<Integer> list =new ArrayList<>();
        /*初始值写死*/
        list.add(10);
        list.add(9);
        list.add(8);
        list.add(8);
        list.add(1);
        list.add(40);
        list.add(42);
        list.add(50);
        list.add(30);
        list.add(35);

        // model.addAttribute("stat", ratingService.getMyRatingStat(user));
        model.addAttribute("stat", list);
        return model;
    }

}
